package org.xiangfei.demo.netty;

import java.util.logging.Level;
import java.util.logging.Logger;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.xiangfei.demo.netty.codec.Auth;

public class AuthServerHandler extends ChannelInboundHandlerAdapter{
    private Logger logger=Logger.getLogger(AuthServerHandler.class.getName());

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception {
        logger.log(Level.INFO, "AuthServerInitHandler channelRead");
        Auth.AuthRequest request=(Auth.AuthRequest)msg;
        System.out.println("request: userId="+request.getUserId()+", password="+request.getPassword());
        Auth.AuthResponse response=Auth.AuthResponse.newBuilder()
                                                        .setResultCode(0)
                                .setResultMessage("success")
                                .build();
        ctx.writeAndFlush(response);
        //ctx.close();
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        logger.log(Level.INFO, "AuthServerInitHandler channelReadComplete");
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {
        logger.log(Level.INFO, "AuthServerInitHandler exceptionCaught");
        cause.printStackTrace();
        ctx.close();
    }   
}